package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Permission;
import com.woniu.supermarket.mapper.PermissionMapper;
import com.woniu.supermarket.mapper.RoleMapper;
import com.woniu.supermarket.service.IPermissionService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    RoleMapper roleMapper;

    @Override
    public int addPermission(Permission permission) {
        int i = permissionMapper.insert(permission);
        permission.setCode(CodeCreateUtil.createPermissionCode(permission.getId()));
        permission.setInsertTime(new Date());
        permission.setUpdateTime(new Date());
        permissionMapper.updateByPrimaryKey(permission);
        return i;
    }

    @Override
    public int modifyPermission(Permission permission) {
        int i = permissionMapper.updateByPrimaryKey(permission);
        return i;
    }

    @Override
    @Transactional
    public int removePermission(int id) {
        int i = permissionMapper.deleteByPrimaryKey(id);
        roleMapper.deleteEmployeeRoleById(id);
        return i;
    }

    @Override
    public PageInfo<Permission> findPermissionByCons(Integer now, Integer size, String name) {
        PageHelper.startPage(now,size);
        List<Permission> lists = permissionMapper.selectByCons(name);
        return new PageInfo<>(lists);
    }


    /**
     * 查询菜单
     * @return
     */
    @Override
    public List<MenuVo> queryMenu() {
        return permissionMapper.selectAllMenu();
    }

    /**
     * 查询父菜单
     * @return
     */
    @Override
    public List<Permission> queryParentMenu() {

        return permissionMapper.selectParentMenu();
    }

    /**
     * 查询某个员工的菜单
     * @param username
     * @return
     */
    @Override
    public List<MenuVo> queryMenuOfSomeone(String username) {
        return permissionMapper.selectMenuOfSomeone(username);
    }

    /**
     * 查询所有权限
     * @return
     */
    @Override
    public List<MenuVo> queryAllPermissions() {

        return permissionMapper.selectAllPermission();
    }

    /**
     * 根据用户名查询用户的所有权限
     * @param username
     * @return
     */
    @Override
    public List<String> queryPermissionsOfOne(String username) {
        return permissionMapper.selectPermissionsOfOne(username);
    }

    @Override
    public List<String> queryPermissionsForSuperAdmin() {
        return permissionMapper.selectPermissionsForSuperAdmin();
    }
}
